<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品分类" prop="productTypeId">
        <el-select v-model="queryParams.productTypeId" placeholder="请选择" clearable size="small">
          <el-option v-for="type in protypeList" :key="type.id" :label="type.name" :value="type.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="库存数量" prop="productFanStore">
        <el-input v-model="queryParams.productFanStore" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品价格" prop="productFanPrice">
        <el-input v-model="queryParams.productFanPrice" placeholder="请输入" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['gongyuan:product:add']">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="产品名称" prop="productName" :show-overflow-tooltip="true" />
      <el-table-column label="产品分类" :show-overflow-tooltip="true" :formatter="typeFormatter" />
      <el-table-column label="商品价格(方案)" prop="productFanPrice" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <span>{{ scope.row.productFanPrice==''||scope.row.productFanPrice==undefined?'--':scope.row.productFanPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存(方案)" prop="productFanStore" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <span>{{ scope.row.productFanStore==''||scope.row.productFanStore==undefined?'--':scope.row.productFanStore }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品价格(普通)" prop="productPtoPrice" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <span>{{ scope.row.productPtoPrice==''||scope.row.productPtoPrice==undefined?'--':scope.row.productPtoPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存(普通)" prop="productPtoStore" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <span>{{ scope.row.productPtoStore==''||scope.row.productPtoStore==undefined?'--':scope.row.productPtoStore }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-info" @click="handleDetail(scope.row)" v-hasPermi="['gongyuan:product:byid']">详情</el-button>
          <el-button class="gongyuan-update" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="['gongyuan:product:edit']">修改</el-button>
          <el-button class="gongyuan-delete" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['gongyuan:product:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getProList, delPro } from '@/api/product/pro'
import { getProtypeList } from '@/api/product/protype'
export default {
  name: 'Prolist',
  data() {
    return {
      // 遮罩层
      loading: true,
      list: [],
      total: 0,
      // 查询参数
      queryParams: {
        productName: '',
        productTypeId: undefined,
        productFanStore: '',
        productFanPrice: '',
        productPtoPrice:'',
        productPtoStore:'',
        pageNum: 1,
        pageSize: 10
      },
      protypeList: []
    }
  },
  created() {
    this.initTypeList()
    this.getList()
  },
  methods: {
    async initTypeList() {
      try {
        const res = await getProtypeList({
          pageNum: 1,
          pageSize: 100000
        })
        if (res.code === 200) {
          this.protypeList = res.rows
        }
      } catch (error) {
        console.log(error)
      }
    },
    typeFormatter(row) {
      return row.productType.name
    },
    async getList() {
      this.loading = true
      const res = await getProList(this.queryParams)
      this.list = res.rows
      // console.log(this.list);

      this.total = res.total
      this.loading = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    handleAdd() {
      // 此处后台管理添加的路由, path对应组件name须一致,不可再修改
      this.$router.push({ name: 'Proedit' })
    },

    handleDetail(row) {
      this.$router.push({ name: 'Prodetail', query: { id: row.productId } })
    },
    handleEdit(row) {
      this.$router.push({ name: 'Proedit', query: { id: row.productId } })
    },
    handleDelete(row) {
      this.$confirm('确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return delPro(row.productId)
        })
        .then(() => {
          this.queryParams.pageNum = 1
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    }
  }
}
</script>
